<?php

namespace app\data\controller\api\auth;

use app\data\controller\api\Auth;
use app\data\service\CartService;

/**
 * 购物车数据接口
 * Class Order
 * @package app\data\controller\api\auth
 */
class Cart extends Auth
{
    /**
     * 绑定数据表
     * @var string
     */
    protected $table = 'ShopGoodsCarts';

    /**
     * 控制器初始化
     */
    protected function initialize()
    {
        parent::initialize();
        if (empty($this->user['status'])) {
            $this->error('账户已被冻结，不能操作订单数据哦！');
        }
    }

    /**
     * 获取购物车列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get()
    {
        $result = $this->_query($this->table)->equal('module')->where(['uuid' => $this->uuid])->order('id desc')->page(true, false, false, input('limit', 10));
        if (count($result['list']) > 0) CartService::instance()->buildData($result['list']);
        $this->success('获取购物车数据成功！', $result);
    }

    /**
     * 添加购物车
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        $data = $this->_vali([
            'uuid.default'     => $this->uuid,
            'module.in:0,1' => '购物车模块类型错误',
            'goods_code.require' => '商品编号不能为空',
            'goods_sku.default' => '',
            'num.default' => 1,
        ]);

        $map = ['uuid' => $data['uuid'], 'module' => $data['module'], 'goods_code' => $data['goods_code'], 'goods_sku' => $data['goods_sku']];
        $cart = $this->app->db->name($this->table)->where($map)->find();


        $goods = $this->app->db->name('ShopGoods')->where('code', $data['goods_code'])->find();
        $goodsSku = $this->app->db->name('ShopGoodsItem')->where('goods_code', $data['goods_code'])->where('goods_sku', $data['goods_sku'])->find();
        if (empty($goods)) {
            $this->error('查无该商品');
        }

        if (empty($cart)) {
            if ($data['module']) {
                if ($goodsSku['stock_total'] < 1) {
                    $this->error('库存不足，无法加入购物车');
                }
            } else {
                if ($goods['stock_total'] < 1) {
                    $this->error('库存不足，无法加入购物车');
                }
            }
            $this->app->db->name($this->table)->save($data);
        } else {
            if ($data['module']) {
                if ($goodsSku['stock_total'] < (1 + $cart['num'])) {
                    $this->error('库存不足，无法加入购物车');
                }
            } else {
                if ($goods['stock_total'] < (1 + $cart['num'])) {
                    $this->error('库存不足，无法加入购物车');
                }
            }
            $this->app->db->name($this->table)->where('id', $cart['id'])->inc('num', $data['num'])->save();
        }

        $this->success('加入购物车成功', $data);
    }

    /**
     * 编辑购物车
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $data = $this->_vali([
            'uuid.default'     => $this->uuid,
            'id.require' => '购物车ID不能为空',
            'num.require' => '商品数量不能为空',
        ]);

        // 用户收货地址
        $map = ['uuid' => $data['uuid'], 'id' => $data['id']];
        $cart = $this->app->db->name($this->table)->where($map)->find();
        if (empty($cart)) {
            $this->error('查无该购物车ID', $data);
        }

        $goods = $this->app->db->name('ShopGoods')->where('code', $cart['goods_code'])->find();
        $goodsSku = $this->app->db->name('ShopGoodsItem')->where('goods_code', $cart['goods_code'])->where('goods_sku', $cart['goods_sku'])->find();
        if (empty($goods)) {
            $this->error('查无该商品');
        }
        if ($cart['module']) {
            if ($goodsSku['stock_total'] < $data['num']) {
                $this->error('库存不足，无法加入购物车');
            }
        } else {
            if ($goods['stock_total'] < $data['num']) {
                $this->error('库存不足，无法加入购物车');
            }
        }


        $this->app->db->name($this->table)->where('id', $data['id'])->save(['num' => $data['num']]);

        $this->success('加入购物车成功', $data);
    }

    /**
     * 删除购物车
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function remove()
    {
        $data = $this->_vali([
            'uuid.default'     => $this->uuid,
            'id.require' => '购物车ID不能为空',
        ]);

        $result = $this->app->db->name($this->table)->where($data)->delete();
        if ($result) {
            $this->success('删除购物车成功', $data);
        } else {
            $this->error('删除购物车失败', $data);
        }
    }
}